home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / GraphicsWorkshop / Source / Converters / pbm.m < prev    next >
Encoding:
Text File  |  1992-05-27  |  4.4 KB  |  259 lines

  1. #import <stdio.h>
  2. #import <math.h>
  3. #import <strings.h>
  4. #import <stdlib.h>
  5. #import <streams/streams.h>
  6. #import <appkit/graphics.h>
  7. #import <appkit/NXBitmapImageRep.h>
  8. #import "NXBitmapImageRepControl.h"
  9. #import "ImageControl.h"
  10. #import "pbm.h"
  11.  
  12. @implementation PBM
  13.  
  14.     NXStream        *myStream;
  15.     unsigned char        *data;
  16.     char                type[5];
  17.     int                width, height;
  18.     int                cwid;
  19.     int                iwid;
  20.  
  21. - init
  22. {
  23.     return self;
  24. }
  25.  
  26. - free
  27. {
  28.     return self;
  29. }
  30.  
  31. - killToEOL
  32. {
  33.     while (NXGetc(myStream) != '\n') ;
  34.     return self;
  35. }
  36.  
  37. - getString: (char *)str from: (NXStream *)stream
  38. {
  39.     char        buffer[256];
  40.     
  41.     do {
  42.         NXScanf(stream, "%s", str);
  43.         if (str[0] == '#') {
  44.             NXScanf(stream, "%[^\n]", buffer); (void)NXGetc(stream);
  45.         }
  46.     } while (str[0] == '#');
  47.  
  48.     return self;
  49. }
  50.  
  51. - (int)getIntFrom: (NXStream *)stream
  52. {
  53.     char        buffer[256];
  54.     
  55.     do {
  56.         NXScanf(stream, "%s", buffer);
  57.         if (buffer[0] == '#') {
  58.             NXScanf(stream, "%[^\n]", buffer); (void)NXGetc(stream);
  59.         }
  60.         else break;
  61.     } while (1);
  62.     return(atoi(buffer));
  63. }
  64.  
  65. - (BOOL)readRawFromStream
  66. {
  67.     int                x, shift, curByte;
  68.     unsigned int        c;
  69.  
  70. #ifdef DEBUG
  71.     fprintf(stderr, "Okay, we're off and reading raw data\n");
  72. #endif
  73.  
  74.     shift = 7;
  75.     data[curByte = 0] = 0;
  76.     for (x = 0; x < width * height; x++) {
  77.         c = [self getIntFrom: myStream];
  78. //        NXScanf(myStream, "%d", &c);
  79. //        fprintf(stderr, "%c:", c);
  80.         if (NXAtEOS(myStream)) return NO;
  81.         data[curByte] |= (c << shift);
  82.         if (!(shift--)) {
  83.             shift = 7;
  84.             data[++curByte] = 0;
  85.         }
  86.     }
  87.  
  88.     return YES;
  89. }
  90.  
  91. - (BOOL)readCompressedFromStream
  92. {
  93. #ifdef DEBUG
  94.     fprintf(stderr, "Okay, we're off and reading condensed data\n");
  95. #endif
  96.  
  97.     [self killToEOL];
  98.  
  99.     if (NXRead(myStream, data, sizeof(unsigned char) * ((width * height) / 8)) != ((width * height) / 8)) return NO;
  100.  
  101.     return YES;
  102. }
  103.  
  104. - readFromStream: (NXStream *)stream from: sender;
  105. {
  106.     id        image;
  107.  
  108. #ifdef DEBUG
  109.     fprintf(stderr, "Made it to read\n");
  110. #endif
  111.  
  112.     myStream = stream;
  113.  
  114.     [self getString: type from: myStream];
  115.     width = [self getIntFrom: myStream];
  116.     height = [self getIntFrom: myStream];
  117. //    NXScanf(myStream, "%s %d %d", type, &width, &height);
  118.     cwid = sizeof(unsigned char) * 8;
  119.     iwid = sizeof(unsigned int) * 8;
  120. #ifdef DEBUG
  121.     fprintf(stderr, "Type %s of dimensions %dx%d\n", type, width, height);
  122. #endif
  123.  
  124.     image = [[NXBitmapImageRep alloc]     initData: NULL
  125.                                         pixelsWide: width
  126.                                         pixelsHigh: height
  127.                                         bitsPerSample: 1
  128.                                         samplesPerPixel: 1
  129.                                         hasAlpha: NO
  130.                                         isPlanar: NO
  131.                                         colorSpace: NX_OneIsBlackColorSpace
  132.                                         bytesPerRow: 0
  133.                                         bitsPerPixel: 0];
  134.     if (!image) return nil;
  135.     data = [image data]; 
  136.  
  137.     if (!strcmp(type, "P1")) {
  138.         if (![self readRawFromStream]) {
  139.             [image free];
  140.             return nil;
  141.         }
  142.     }
  143.     else if (!strcmp(type, "P4")) {
  144.         if (![self readCompressedFromStream]) {
  145.             [image free];
  146.             return nil;
  147.         }
  148.     }
  149.     else return nil;
  150.     
  151.     return image;
  152. }
  153.  
  154. - (BOOL)write: (id)image toStream: (NXStream *)stream from: sender;
  155. {
  156.     id                newImage;
  157.     id                imageCon;
  158.     unsigned char        *dataPtr;
  159.     int                size, x;
  160.     
  161.     imageCon = [sender getImageControl: image];
  162. #ifdef DEBUG
  163.     fprintf(stderr, "1...");
  164. #endif
  165.     newImage = [imageCon ditherImage];
  166. #ifdef DEBUG
  167.     fprintf(stderr, "2...");
  168. #endif
  169.     [imageCon free];
  170. #ifdef DEBUG
  171.     fprintf(stderr, "3...");
  172. #endif
  173.     imageCon = [sender getImageControl: image];
  174. #ifdef DEBUG
  175.     fprintf(stderr, "4...\n");
  176. #endif
  177.     
  178.     NXPrintf(stream, "P4\n");
  179.     NXPrintf(stream, "%d %d\n", [newImage pixelsWide], [newImage pixelsHigh]);
  180.     size = [newImage pixelsWide] * [newImage pixelsHigh] / 8;
  181. #ifdef DEBUG
  182.     fprintf(stderr, "%d\n", size);
  183. #endif
  184.     for (x = 0, dataPtr = [newImage data]; x < size; x++) {
  185.         NXPutc(stream, *dataPtr++);
  186.     }
  187.     
  188.     [newImage free];
  189.     
  190.     return YES;
  191. }
  192.  
  193. - readAllFromStream: (NXStream *)stream from: sender
  194. {
  195.     return nil;
  196. }
  197.  
  198. - (BOOL)writeAll: (id)image toStream: (NXStream *)stream
  199. {
  200.     return NO;
  201. }
  202.  
  203. - customSaveView: (int)width
  204. {
  205.     return nil;
  206. }
  207.  
  208. - customOpenView: (int)width
  209. {
  210.     return nil;
  211. }
  212.  
  213. - (char *)getFormatName
  214. {
  215.     return("Portable Bitmap (PBM)");
  216. }
  217.  
  218.  - (BOOL)setCustomParameter: (const char *)parameter withValue: (void *)ptr
  219. {
  220.     return NO;
  221. }
  222.  
  223.  - (void *)getCustomParameter: (const char *)parameter
  224. {
  225.     return nil;
  226. }
  227.  
  228. - (char *)copyrightNotice
  229. {
  230.     return "PBM Converter\nby Alex Raftis\nCopyright (c) 1991 Cal Poly State University\nEmail bugs to alex@data.ACS.CalPoly.EDU";
  231. }
  232.  
  233. - (int)errorState
  234. {
  235.     return CONVERT_ERR_NONE;
  236. }
  237.  
  238. - (int)errorMessage
  239. {
  240.     return ERROR_NO_ERROR;
  241. }
  242.  
  243. - (char *)errorStringMessage
  244. {
  245.     return NULL;
  246. }
  247.  
  248. - (BOOL)needsWindowServer;
  249. {
  250.     return NO;
  251. }
  252.  
  253. - (char *)protocolVersion
  254. {
  255.     return "1.0";
  256. }
  257.  
  258. @end
  259.